import json
import os
import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

from common.doLog import DoLog

my_path=r"D:\work\pythonProject1\datas"
my_path1=r"D:\work\pythonProject1\datas"
dl=DoLog()
myLogger=dl.getLogger(__name__)
wk=None
class DoTxt:
    def doTxtRead(self):
        fileName = r"D:\work\pythonProject1\datas"
        try:
            myLogger.info("开始运行")
            with open(fileName,encoding="utf-8")as f:
                d=f.read()
        except Exception as e:
            print(f"发生了异常，异常信息是{e}")
            myLogger.exception(f"发生了异常，异常信息是{e}")
        else:
            print("未发生异常")
            myLogger.info("未发生异常")
            print(d)
            return True
        finally:
            print("处理结束了")
            myLogger.info("处理结束了")
    def doTxtWrite(self):
        fileName = r"D:\work\pythonProject1\datas"
        try:
            myLogger.info("开始运行")
            f=open(fileName,mode="a",encoding="utf-8")
        except Exception as e:
            print(f"{fileName}文件发生了异常，异常信息是{e}")
            myLogger.exception(f"{fileName}文件发生了异常，异常信息是{e}")
        else:
            print(f"处理文件{fileName}未发生异常")
            myLogger.info(f"处理文件{fileName}未发生异常")
            f.write("abcd")
            f.close()
            return True
        finally:
            print(f"{fileName}处理结束了")
            myLogger.info(f"{fileName}处理结束了")
    #     字典内容转入json文件中
    def doDictToJsonFile(self,d,fname):
        fileName = os.path.join(my_path1,fname)
        try:
            myLogger.info("开始运行")
            with open(fileName,mode="w",encoding="utf-8")as f:
                d=json.dump(d,f)
        except Exception as e:
            print(f"{fileName}文件发生了异常，异常信息是{e}")
            myLogger.exception(f"{fileName}文件发生了异常，异常信息是{e}")
        else:
            print(f"处理文件{fileName}未发生异常")
            myLogger.info(f"处理文件{fileName}未发生异常")
            print(d,type(d))
        finally:
            print(f"{fileName}处理结束了")
            myLogger.info(f"{fileName}处理结束了")
    # json转字典
    def doJsonToDictFile(self,fname):
        fileName = os.path.join(my_path1,fname)
        try:
            myLogger.info("开始运行")
            with open(fileName,mode="r",encoding="utf-8")as f:
                d=json.load(f)
        except Exception as e:
            print(f"{fileName}文件发生了异常，异常信息是{e}")
            myLogger.exception(f"{fileName}文件发生了异常，异常信息是{e}")
        else:
            print(f"处理文件{fileName}未发生异常")
            myLogger.info(f"处理文件{fileName}未发生异常")
            print(d,type(d))
            return d
        finally:
            print(f"{fileName}处理结束了")
            myLogger.info(f"{fileName}处理结束了")

    def doExcelRead(self,fname,stname,minRow=None,maxRow=None,minCol=None,maxCol=None):
        fileName=os.path.join(my_path1,fname)
        try:
            myLogger.info("开始运行")
            wk:Workbook=openpyxl.load_workbook(fileName)
            st:Worksheet=wk[stname]
            print(st,type(st))

            ss=st.iter_rows(min_row=minRow,max_row=maxRow,min_col=minCol,max_col=maxCol,values_only=True)
        # for i in ss:
        #     print(i)
            print("开始运行")
        except Exception as e:
            print(f"{fileName}文件,sheet名{stname}处理发生了异常，异常信息是{e}")
            myLogger.exception(f"{fileName}文件,sheet名{stname}处理发生了异常，异常信息是{e}")
        else:
            print(f"处理文件{fileName},{stname}未发生异常")
            myLogger.info(f"处理文件{fileName},{stname}未发生异常")
            l=[i for i in ss]
            print(f"{l}")
        finally:
            print(f"{fileName}处理结束了")
            myLogger.info(f"{fileName}处理结束了")
            wk.close()
            return l

    def doExcelWrite(self,fname,stname,ss):
        fileName=os.path.join(my_path1,fname)
        try:
            myLogger.info("开始运行")
            wk: Workbook = openpyxl.Workbook()
            st:Worksheet=wk.create_sheet(stname,1)
        except Exception as e:
            print(f"{fileName}文件,sheet名{stname}处理发生了异常，异常信息是{e}")
            myLogger.exception(f"{fileName}文件,sheet名{stname}处理发生了异常，异常信息是{e}")
        else:
            print(f"处理文件{fileName},{stname}未发生异常")
            myLogger.info(f"处理文件{fileName},{stname}未发生异常")
            st.cell(1,1,ss)
        finally:
            print(f"{fileName}处理结束了")
            myLogger.info(f"{fileName}处理结束了")
            wk.save(fileName)



if __name__ == '__main__':
    df=DoTxt()
    # df.doTxtRead()
    # df.doTxtWrite()
    # d={"name":"zhangsan","age":20}
    # df.doDictToJsonFile(d,"myJson.json")
    # df.doJsonToDictFile("myJson.json")
    # df.doExcelRead("apt.xlsx","Sheet1",2,7,4,6)
    l = df.doExcelRead("apt.xlsx", "Sheet2", 2, 7, 4, 6)
